package cn.wh.context.support;

import cn.wh.beans.BeansException;
import cn.wh.beans.factory.config.BeanPostProcessor;
import cn.wh.context.ApplicationContext;
import cn.wh.context.ApplicationContextAware;

/**
 * @Author wanghao
 * @Date 2022-09-21 21:27
 * @Description: TODO
 */
public class ApplicationContextAwareProcessor implements BeanPostProcessor {

   private final ApplicationContext applicationContext;

    public ApplicationContextAwareProcessor(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }



    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ApplicationContextAware){
            ((ApplicationContextAware)bean).setApplicationContext(applicationContext);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}
